package com.fg.cloud.manager.service.system.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.manager.entry.BaseEntity;
import com.fg.cloud.manager.entry.system.RoleEntity;
import com.fg.cloud.manager.mapper.BaseMapper;
import com.fg.cloud.manager.mapper.system.RoleMapper;
import com.fg.cloud.manager.service.BaseServiceImpl;
import com.fg.cloud.manager.service.system.RoleService;

/**
 * @author ：
 * @date ： 2017年6月16日 上午11:00:20
 * @Desc ：系统角色service
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl implements RoleService {

	@Autowired
	private RoleMapper roleMapper;

	@Override
	protected <T extends BaseEntity> BaseMapper getBaseMapper() {
		return roleMapper;
	}

	@Override
	@Transactional
	public int delete(Long id) {
		// 成功后级联删除用户角色表t_factory_user_role、t_system_role_menu
		int count=roleMapper.deleteMenuRole(id);
		count+=roleMapper.deleteUserRole(id);

		return count;
	}

	@Override
	@Transactional
	public boolean updateMenuRoles(Long roleId, String menuIds) {
		List<Long> list = roleMapper.selectMenuIdByRoleId(roleId);
		Long[] deletes = menus(LongUtils.convert(menuIds.split(",")), list.toArray(new Long[list.size()]));
		Long[] adds = menus(list.toArray(new Long[list.size()]),LongUtils.convert(menuIds.split(",")));
		int count = 0;
		if (deletes.length!=0) {
			count = roleMapper.deleteSpecifiedMenuRole(roleId, deletes);
		}
		if (adds.length!=0) {
			count += roleMapper.batchInsert(roleId, adds);
		}
		return count >= 0 ? true : false;
	}

	/**
	 * Description:新旧关联比较
	 * 	新数组在前 返回删除    新数组在后   返回新增
	 * @param news
	 * @param olds
	 * @return String[]
	 * @author shaoqiping
	 * @date 2017年8月11日上午8:52:31
	 */
	public Long[] menus(Long[] param1, Long[] param2) {
		List<Long> list= new ArrayList<Long>();
		for (Long i : param2) {
			int temp=0;
			for (Long j : param1) {
				if(i==j){
					break;
				}
				temp++;
			}
			if(temp==param1.length){
				list.add(i);
			}
		}
		return list.toArray(new Long[list.size()]);
	}

	@Override
	public Set<String> findByUserId(Long userId) {
		return roleMapper.selectRoleByUserId(userId);
	}

	@Override
	public RoleEntity findByRoleName(RoleEntity entity) {
		return roleMapper.selectByRoleName(entity);
	}

	@Override
	@Transactional
	public boolean deletes(Map<String, Object> map) {
		Long[] array = LongUtils.convert(((String) map.get("ids")).split("[, ]+"));
		roleMapper.deleteMenuRoles(array);
		roleMapper.deleteUserRoles(array);
		map.put("ids", array);
		int temp = roleMapper.deletes(map);
		return temp > 0 ? true : false;
	}

	@Override
	public void findList(PageUtils<RoleEntity> pageinfo) {
		long total = roleMapper.filterCount(pageinfo.getCondition());
		pageinfo.setTotal(total);
		if (total > 0) {
			pageinfo.setData(roleMapper.findList(pageinfo));
		} else {
			pageinfo.setData(new ArrayList<RoleEntity>());
		}

	}

	@Override
	public RoleEntity findToCheck(Long id) {
		return roleMapper.selectToCheck(id);
	}

	@Override
	public void sysFindList(PageUtils<RoleEntity> pageinfo) {
		long total = roleMapper.sysFilterCount(pageinfo.getCondition());
		pageinfo.setTotal(total);
		if (total > 0) {
			pageinfo.setData(roleMapper.sysFindList(pageinfo));
		} else {
			pageinfo.setData(new ArrayList<RoleEntity>());
		}
	}

	@Override
	@Transactional
	public boolean sysDeletes(String ids) {
		Long[] array = LongUtils.convert(ids.split("[ ,]"));
		roleMapper.deleteMenuRoles(array);
		roleMapper.deleteUserRoles(array);
		int temp = roleMapper.sysDeletes(array);
		return temp>0;
	}

}
